package Set;

import java.util.HashSet;
import java.util.Objects;

/**
 * @description:
 * @Author yyp
 * @time 2022/3/25
 */
public class HashSet_ {
    public static void main(String[] args) {
//        HashSet set = new HashSet();
//        String a = new String("a");
//        set.add(a);
//        a = "b";
//        System.out.println(a);

        HashSet hashSet = new HashSet();
        Person p1 = new Person("a",10);
        Person p2 = new Person("b",50);
        hashSet.add(p1);hashSet.add(p2);
        System.out.println(hashSet);
        p1.name = "aaa";
        hashSet.remove(p1);
        hashSet.add(p1);
        System.out.println(hashSet);
        hashSet.add(p1);
        for (Object o : hashSet) {
            System.out.println(o.toString());
        }
    }
}

class Person{
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}'+hashCode();
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    String name;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
